package com.richard.base.net.interceptors;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @date 创建时间：2019-10-30
 * @auther wuzhiguo
 * @Description 网络请求超时时间动态设置
 */
public class DynamicTimeoutInterceptor implements Interceptor {

    public static final int MINI_TIME = 5;

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request = chain.request();
        boolean needMiniTimeout = ("1".equals(request.header("xTimeout")));

        if (needMiniTimeout) {
            return chain.withConnectTimeout(MINI_TIME, TimeUnit.SECONDS)
                    .withReadTimeout(MINI_TIME, TimeUnit.SECONDS)
                    .withReadTimeout(MINI_TIME, TimeUnit.SECONDS)
                    .proceed(request);
        }

        return chain.proceed(request);
    }
}
